package com.example.kotlindemo.委托

import java.sql.Ref
import kotlin.properties.Delegates
import kotlin.reflect.KProperty

interface IUserAction {
    fun defense()

    fun attack()
}

class UserActionImpl: IUserAction{
    override fun defense() {
        println("defense")

    }

    override fun attack() {
        println("attack")
    }

}

class UserActionImpl2: IUserAction{
    override fun defense() {
        println("defense2")

    }

    override fun attack() {
        println("attack2")
    }

}



class UserDelegate(private val action: IUserAction):IUserAction by action
//深刻理解代理的意思 本质上还是 Java的组合模式 理解


fun main() {
    val actionImpl = UserActionImpl()


    UserDelegate(actionImpl).run {
        attack()
        defense()
    }

    println(textStr)
    println(name)
    println(name)

    values = "1"
    values = "2"

}

//委托属性
private val textStr by  TextDelegate()

class TextDelegate{
    operator fun getValue(thisRef: Any?,property:KProperty<*>):String{
        return "hehe hello !!"
    }
//    operator fun setValue(Nothing?, KProperty<*>, String){
//
//    }
}

//bylazy
val name:String by lazy {
    println("第一次调用初始化")
    "luge"
}

//都是Kotlin 帮我实现了一些功能
var values:String by Delegates.observable("defalueValue"){
    property, oldValue, newValue ->
    println("$oldValue - > $newValue")
}

val test:String by Delegates.notNull<String>()


